package com.why.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.why.dao.ProductMapper;
import com.why.pojo.Product;
import com.why.service.ProductService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    private SolrClient solrClient;

    @Override
    public PageInfo<Product> productList(Integer categoryLevelId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> list = productMapper.productList(categoryLevelId);
        PageInfo<Product> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Product> selectProductList() {
        return productMapper.selectProductList();
    }

    @Override
    public Product selectByPrimaryKey(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateStock(Integer stock ,String id) {
        if (productMapper.updateStock(stock,id)==1){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteProduct(Integer id) throws IOException, SolrServerException {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setIsdelete(1);
        if (productMapper.deleteProduct(id)==1){

            solrClient.addBean(product);
            solrClient.commit();
            return true;
        }
        return false;
    }

    @Override
    public boolean insertSelective(Product record) throws IOException, SolrServerException {
        record.setIsdelete(0);
        if (productMapper.insertSelective(record)==1){
            solrClient.addBean(record);
            solrClient.commit();
            return true;
        }
        return false;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Product record) throws IOException, SolrServerException {
        Product product = productMapper.selectByPrimaryKey(Integer.parseInt(record.getId()));

        //当不上传文件时
        if (record.getFilename()==null){
            record.setFilename(product.getFilename());
        }
        record.setIsdelete(product.getIsdelete());
        if (productMapper.updateByPrimaryKeySelective(record)==1){
            solrClient.addBean(record);
            solrClient.commit();
            return true;
        }
        return false;
    }


    @Override
    public PageInfo<Product> selectAllProduct(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> list = productMapper.selectAllProduct();
        PageInfo<Product> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean putaway(Integer id) throws IOException, SolrServerException {
        Product product = productMapper.selectProByIdIsDel(id);
        product.setIsdelete(0);
        if (productMapper.putaway(id)==1){
            solrClient.addBean(product);
            solrClient.commit();
            return true;
        }
        return false;
    }

    @Override
    public Product selectProByName(String name) {
        return productMapper.selectProByName(name);
    }

}
